<template>
  <!-- 对话框(添加 / 修改) -->
  <el-dialog
    :title="title"
    :visible.sync="show"
    width="500px"
    append-to-body
    :before-close="cancel"
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="薪酬" prop="remuneration">
        <el-input
          type="number"
          v-model="form.remuneration"
          placeholder="请输入薪酬"
        />
      </el-form-item>
      <el-form-item label="学院" prop="collegeId">
        <college-selector v-model="form.collegeId"></college-selector>
      </el-form-item>
      <el-form-item label="系部" prop="departmentsId">
        <departments-selector
          v-model="form.departmentsId"
          :college-id="form.collegeId"
        ></departments-selector>
      </el-form-item>
      <el-form-item label="教师" prop="teacherId">
        <teacher-selector
          v-model="form.teacherId"
          :college-id="form.collegeId"
        ></teacher-selector>
      </el-form-item>
      <el-form-item label="薪酬备注" prop="remunerationRemark">
        <el-input
          type="textarea"
          :rows="2"
          v-model="form.remunerationRemark"
          placeholder="请输入薪酬备注"
        />
      </el-form-item>
      <!--  <el-form-item label="机构编号" prop="orgId">
                        <el-input v-model="form.orgId" placeholder="请输入机构编号" />
                      </el-form-item> -->
      <!--  <el-form-item label="备注" prop="remark">
                        <el-input v-model="form.remark" placeholder="请输入备注" />
                      </el-form-item>-->
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { createTeacherRemuneration, updateTeacherRemuneration } from "@/api/collegebase/teacherRemuneration";
import CollegeSelector from "@/components/CollegeSelector";
import DepartmentsSelector from "@/components/DepartmentsSelector";
import TeacherSelector from "@/components/TeacherSelector";

export default {
  name: "TeacherRemunerationEdit",
  components: {
    TeacherSelector,
    DepartmentsSelector,
    CollegeSelector
  },
  data() {
    return {
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        remuneration: [{ required: true, message: "薪酬不能为空", trigger: "blur" }],
        collegeId: [{ required: true, message: "学院id不能为空", trigger: "blur" }],
        departmentsId: [{ required: true, message: "系部id不能为空", trigger: "blur" }],
        teacherId: [{ required: true, message: "教师id不能为空", trigger: "blur" }],
      }
    };
  },
  created() { },
  props: {
    // 是否显示弹出层
    show: {
      type: Boolean,
      default: false
    },
    // 显示弹出层标题
    title: {
      type: String,
      default: ''
    }
  },
  methods: {
    /** 初始化编辑的数据**/
    initData(rowData) {
      this.reset();
      if (rowData && rowData.id) {
        this.form = rowData;
        this.form.datetimerange = [this.form.startTime, this.form.endTime]
      }
    },
    /** 取消按钮 */
    cancel() {
      this.$emit('update:show', false)
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        remuneration: undefined,
        collegeId: undefined,
        departmentsId: undefined,
        teacherId: undefined,
        remunerationRemark: undefined,
        orgId: undefined,
        remark: undefined,
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateTeacherRemuneration(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.cancel();
            this.$emit('success')
          });
          return;
        }
        // 添加的提交
        createTeacherRemuneration(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.cancel();
          this.$emit('success')
        });
      });
    },

  }
};
</script>
